#include"Include.h"

vu8 OvpLock_Cnt=0;


void __attribute (( interrupt(0x04) ))  ISR_CMP4(void) // 
{
	_cp4f = 0;	

	bOVP_Adj = 1;
	if(bStartPPG==1)
	{
		bStartPPG = 0;
//    	_pst0=1;    //
//		_prsen=1;  //	
		LowPow_Init();
   		_t1on = 1;
   	}
   	if(bLowPower == 1)
   	{
   		_t1on = 1;	
   	}
   	
  	if(bLowPower==1 && (1==bPowerState) )
  	{	
 		if(OvpLock_Cnt>=C_OVPLOCK_CNT)  //ovp Software debounce
		{
			LowPow_Cnt++;	
			OvpLock_Cnt=0;
		}
  		LowPow_EndTime_Cnt=0;
  	}
  	else
  	{
  		// disable  PPG
		LowPow_Cnt=0;
  		Statu_LowPow_Work=0;
		LowPow_EndTime_Cnt = 0;
  	}
  	
	if(1==Statu_LowPow_Work )
	{			
		LowPow_Init();		
	}	
}

//125us interrupt
void __attribute( ( interrupt(0x24) ) ) ISR_TMR0_TMR1 (void)  
{
	static vu8 Timer1mS=0;
	static vu8 Timer10mS=0;
	
	if(_t0f)//125us
	{
		Timer1mS++;
		if(Timer1mS>=8)			//1ms
		{
			Timer1mS =0;
			bT1mS_Event=1;
			OvpLock_Cnt++;  
		}	
		#if BUZZER_IO_TYPE == 0
			if(1==bBeep_ON)
			{	
				BEEP_ON();			
			}		
		#endif
		
		_t0f = 0;		
	}
	//----------------------------
	if(_t1f)//150us
	{			
		LowPow_Control();		//ppg2 ON
		
		if(bOVP_Adj == 0)
		{		
			Adj_Power(); 
	
			Timer10mS = 0;
		}
	    else if(bOVP_Adj == 1)
		{
			Timer10mS++;    
	 		if(Timer10mS==12)  	//12*150us=1.8ms
			{
				Timer10mS = 0;
			    bOVP_Adj = 0;	
			    bPowerAdjStart=1;
		    }		
		}
		_t1f = 0;		
	}
			
}

void __attribute (( interrupt(0x14) ))  ISR_ADC(void)
{
	
	Ad_ChannelIndex++;	    
	bAD_Convert_Ok = 1; // A piece of data OK	
}


//Reverse Voltage Protection
void __attribute (( interrupt(0x08) ))  ISR_CMP1(void)
{
	_cp1f=0;
	
	
	if(PPGTA_Min>=C_MaxLoadShedPPGT)
	{
		PPGTA_Min = C_MaxLoadShedPPGT;
	}
	else
	{
		PPGTA_Min = PPGTA_Min+5;	
	}
	//記錄當前ppgta值加一個參數，作為PPGTA—Max值
	
	u16ErrorBuf |=0b0010000000000000;	//8192
}

////----------Surge  -----------------------
void __attribute (( interrupt(0x18) ))  ISR_CMP2(void)
{
	//--------------Surge-----------------------
	bSurgeHW=1;	
	ErrRecoveryTimer=C_ErrRecoveryTime;	
	
	_cp2f=0;
}


void __attribute (( interrupt(0x1c) ))  ISR_CMP3(void)
{
	//_pc3=~_pc3;
//	_pb0=~_pb0;	
//	u16ErrorBuf |=0b0000000001000000;	//64

	_cp3f=0;		
}
//Current Surge
void __attribute (( interrupt(0x28) ))  ISR_CMP5(void)
{

//	_pb0=~_pb0;
	bCurrentSurgeHW = 1;
	_cp5f=0;		
	
	u16ErrorBuf |= 0b0000000000000100;	//4
}



void __attribute((interrupt(0x34))) ISR_PPGATCD(void)
{
	_ppgatcdf=0;
	_ppgatcde=0;		
}


void __attribute((interrupt(0x38))) ISR_PPGRETRI(void)
{
	_ppgrnf=0;
		
	_ppgsamd = 0;
    _ppgsaen = 0;
	//PPG2M0~1:00  ppg2  = 0
	_ppg2m1=0;			
	_ppg2m0=0;			
	
	_prsen=1;
	_c0en=1;	

	if(bLowPower)
	{
		Statu_LowPow_Work=2; //ppg2 WORKING	flag
		bLowPowerADJ=1;//Power ADJ Flag	
	}
	else
	{
	    PPGTA_copy = C_InitPPGT;
	    while (_pst0 == 1); // Wait PST bit low
	    if (PPGTA_copy > 255)
	    {
	        _ppgta8 = 1;
	    }
	    else
	    {
	        _ppgta8 = 0;
	    }
	    _ppgta = PPGTA_copy & 0XFF; //	
	}

}
